<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4.代理模式典型应用之虚拟代理（图片预加载）</title>
</head>
<style>
    #imgBox{
        width: 500px;
    }
    .imgWrap{
        width: 100%;
        height: 450px;
    }
</style>
<body>
    <div id="imgBox">
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        <div class="imgWrap">
            <img src="http://localhost:8888/zhanwei" alt="" srcset="" width="100%" height="100%">
        </div>
        
    </div>

    <script>
        let imgs = document.getElementsByTagName('img');
        // console.log('imgs:',typeof imgs);
        let lazyLoading = function () { 
            [...imgs].forEach((img,i)=>{
                let imgTop = img.getBoundingClientRect().top;
                let clientHieght = window.innerHeight || document.documentElement.clientHeight;
                if(imgTop < clientHieght){
                    img.src = `http://localhost:8888/images/image${i+1}.jpeg`
                }
            })
         }
         window.addEventListener('scroll',lazyLoading,false);
    </script>
    
</body>
</html>